/*
 * @Author: yuqy
 * @Date: 2023-04-11 13:49:33
 * @LastEditors: yuqy
 * @LastEditTime: 2023-10-24 15:43:07
 * @Descripttion: -
 */
import { getAction, postAction } from "@/api"
// import route from '@/router'
import { getVideoEncoder } from '@/libs/videoEncode'
import { Message } from "view-ui-plus"
export default {
    namespaced: true,
    state: {
        agoraToken: '',
        appid: '',
        channelName: '',
        lowVideoEncoder: '720p_5', // 听讲选择的频率
        highVideoEncoder: '720p_2', // 主讲选择的推送级别
        
        audioencoderConfig: 'speech_low_quality', // 音频采集基本
        audioencoderElectronConfig: 'AudioProfileDefault',
        deviceAudioId: '', // 选中的麦克风设备
        deviceVideoId: null, // 选中的摄像头 为空默认没有摄像头
        roomInfo: null, // 当前房间信息
        sreenUserList: [{uid: 1, name: '111', isfull: false}, {uid: 2, name: '222', isfull: false}, {uid: 3, name: '333', isfull: false}, {uid: 4, name: '444', isfull: false}], // 当前房间听众列表
        showLiveing: false,// 是否开启了直播
        liveUrl: null, // 当前房间的直播链接
        liveData: {} // 当前直播信息
    },
    getters: {
        getsreenUserList: (state) => {
            return state.sreenUserList
        },
        getDemo: (state) => {
            return '今天是个好日子哦'
        },
        getLowVideoEncoder: (state) => {
            let label = state.lowVideoEncoder;
            return getVideoEncoder(label);
        },
        getHighVideoEncoder: (state) => {
            let label = state.highVideoEncoder;
            return getVideoEncoder(label);
        },
        getAudioencoderConfig: (state) => {
            let label = state.audioencoderConfig;
            return label;
        }
    },
    actions: {
        /**
         * @name: 听讲主讲退出当前房间 清空房间信息
         * @msg: type 1 主讲  2是听讲
         * @return {*}
         */
        downOutClass({ commit }, { code, type = 1 }) {
            return new Promise(async (resolve) => {
                let res = null
                if (type == 1) {
                    res = await postAction('/whiteboardclient/interaction/classOver?code=' + code, { code })
                }
                if (type == 2) {
                    res = await postAction('/whiteboardclient/interaction/outToClassroom?code=' + code, { code })
                }
                // 清空房间信息
                commit('updateRoom', null)
                // 清空声网信息
                commit('updateAgora', { token: null, appid: null, channelName: null })
                resolve(res)
            })
        },
        
        /**
         * @name: 获取当前 房间信息直播信
         * @msg: 
         * @return {*}
         */
        getRoomLiveInfo({ commit }, params) {
            return new Promise(async (resolve, reject) => {
                let { success, data } = await getAction('/whiteboardclient/interaction/getLiveUrl', params)
                if (success && data && data.liveUrl) {
                    commit('updateLiveUrl', data.liveUrl)
                    commit('updateRoom', data)
                    resolve(true)
                } else {
                    commit('updateLiveUrl', null)
                    resolve(false)
                }
            })
        },

        /**
         * @name: 获取当前 房间信息
         * @msg: 
         * @return {*}
         */
        getRoomInfo({ commit }, code) {
            return new Promise(async (resolve, reject) => {
                // 初始值是空
                commit('updateSreenUser', [])
                let { message, classroomIn, agoraInteractionInfo, liveUrl } = await getAction('/whiteboardclient/interaction/getClassroom', { code })
                if (message) {
                    Message.error(message)
                    return reject(false)
                }
                if (classroomIn.speakerPhone) {
                    let regPhone = /^1[123456789][0-9]{9}$/
                    let list = []
                    if (classroomIn.listenerInfoList) {
                        classroomIn.listenerInfoList.forEach(element => {
                            element.isfull = false // 是否处于全屏状态
                            element.isAudio = false // 是否在语音状态
                            element.isApplySpeak = false // 是否处于申请发言状态
                            element.playStatus = 101 // 默认处于视频获取中
                            element.activeTimeStamp = 0 // 标记这种的是预约用户，活跃状态为沉默
                            // 听众的uid 需要有效的手机号
                            if (regPhone.test(element.phone)) {
                                element.uid = '1' + element.phone.substring(7, 11)
                                list.push(element)
                            }
                        })
                    }
                    // 更新房间初始人员信息
                    commit('updateSreenUser', list)
                    // 更新房间信息
                    commit('updateRoom', classroomIn)
                    // 更新声网信息
                    if (agoraInteractionInfo && agoraInteractionInfo.token) {
                        commit('updateAgora', agoraInteractionInfo)
                    }
                    commit('updateLiveUrl', liveUrl)
                    commit('updateLiveData', { bool: liveUrl?true:false, converter: {}})
                    resolve(true)
                } else {
                    reject(false)
                    Message.error('当前房间信息缺失, 请退出重试!')
                }
            })
        },
        // 创建课堂
        creatRoom({ commit }, speakerPhone) {
            return new Promise(async (resolve, reject) => {
                let params = {
                    deviceId: "1",
                    speakerPhone
                }
                let res = await getAction("/whiteboardclient/interaction/createClass", params);
                if (res && res.code && !isNaN(res.code)) {
                    commit("updateAgora", res)
                    Message.success({ content: '创建成功！' });
                    // 需要把房间信息清空,房间内的原因
                    commit("updateRoom", null)
                    // 更新房间内人员信息
                    commit("updateSreenUser", [])
                    resolve(res.code)
                } else {
                    Message.error({ content: '课程码异常或者不存在' });
                    reject(false)
                }
            })
        },
        // 加入课堂
        joinRoom({ commit }, code) {
            return new Promise(async (resolve, reject) => {
                let { classroomIn } = await getAction('/whiteboardclient/interaction/joinClassm', { code })
                if (classroomIn.speakerPhone && agoraTokenWithDeviceRsp.token) {
                    commit('updateRoom', classroomIn)
                    resolve(classroomIn)
                } else {
                    reject(false)
                    Message.error('当前房间信息缺失, 请退出重试!')
                }
            })
        },

        actionsDemo({ commit }) {
            commit('updateAgora', { token: '1111', appid: '1111', channelName: '2333' })
        },
        // 开启直播
        startLiveStream({ commit }, params) {
            return new Promise(async (resolve, reject) => {
                let { converter } = await getAction('/whiteboardclient/interaction/pushCurrent', params)
                if (!!converter) {
                    commit('updateLiveData', { bool: true, converter })
                    resolve(converter)
                } else {
                    resolve(false)
                }
            })
        },
        // 删除直播
        deldeteLiveStream ({ commit }, params) {
            return new Promise(async (resolve, reject) => {
                await getAction('/whiteboardclient/interaction/delLive', params)
                commit('updateLiveData', { bool: false, converter: {} })
                resolve(true)
            })
        }
    },
    mutations: {
        updateLiveUrl (state, value) {
            state.liveUrl = value
        },
        updateLiveData (state, { bool, converter }) {
            state.showLiveing = bool
            state.liveData = converter
        },
        updateSreenUser (state, value) {
            state.sreenUserList = value
        },
        // 更新当前房间信息
        updateRoom(state, value) {
            state.roomInfo = value
        },
        // 更新声网信息
        updateAgora(state, { token, appid, channelName }) {
            state.agoraToken = token
            state.appid = appid
            state.channelName = channelName
        },
        // 更新声网音视频采集配置信息
        updateAudioVideoEncoder(state, { lowVideoEncoder, highVideoEncoder, audioencoderConfig, audioencoderElectronConfig }) {
            state.lowVideoEncoder = lowVideoEncoder
            state.highVideoEncoder = highVideoEncoder
            state.audioencoderConfig = audioencoderConfig
            if (audioencoderElectronConfig) {
                state.audioencoderElectronConfig = audioencoderElectronConfig
            }
        },
        /**
         * @name: 更新选中的麦克风
         * @msg: 
         * @param {*} state
         * @param {*} deviceAudioName
         * @return {*}
         */
        updateAudioMirphone(state, { deviceAudioId }) {
            state.deviceAudioId = deviceAudioId
        },
        /**
         * @name: 更新选中的摄像头
         * @msg: 
         * @param {*} state
         * @param {*} deviceAudioName
         * @return {*}
         */
        updateCamera(state, { deviceVideoId }) {
            state.deviceVideoId = deviceVideoId
        },
        /**
         * @name: 开启关闭直播 
         * @msg: 
         * @return {*}
         */
        updateLiveing(state, bool) {
            state.showLiveing = bool
        },
    }
}